import bean.KnnNode;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class FileReadDemo {

    //用二维数组存放指纹地图的数据
    private static int mapRssi[][];
    private static int locRssi[][];
    private static double location[][] =
            {{1.11,0.17},{1.11,1.24},{1.11,2.27},{1.10,3.30},{1.10,4.33},
                    {1.10,5.56},{1.09,6.59},{1.07,7.73},{1.06,8.76},{1.06,9.77},
                    {2.05,0.17},{2.05,1.24},{2.05,2.27},{2.04,3.30},{2.04,4.33},
                    {2.03,5.56},{2.03,6.59},{2.01,7.73},{2.01,8.76},{2.01,9.77},
                    {2.99,0.17},{2.99,1.23},{2.99,2.27},{2.99,3.30},{2.99,4.33},
                    {2.99,5.56},{2.29,6.59},{2.99,7.73},{2.99,8.76},{2.99,9.77},
                    {3.93,0.17},{3.91,1.23},{3.93,2.27},{3.93,3.30},{3.93,4.33},
                    {3.93,5.56},{3.93,6.59},{3.93,7.73},{3.93,8.76},{3.93,9.77},
                    {4.89,0.17},{4.89,1.23},{4.89,2.27},{4.89,3.30},{4.89,4.33},
                    {4.89,5.56},{4.89,6.59},{4.89,7.73},{4.89,8.76},{4.89,9.77}};

    public static void main(String[] args) {
        mapRssi = new int[50][10];
        Scanner in = new Scanner(System.in);
        System.out.println("请输入指纹地图的文件路径：");
        String targetPath = in.nextLine();
        String content = FilesHandler.readFile(targetPath);
        txtHandler(content);
        for (int i = 0; i < 50; i++) {
            for (int j = 0; j < 10; j++) {
                System.out.print(mapRssi[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("请输入要检测的定位点个数：");
        int n = in.nextInt();
        locRssi = new int[n][10];
        System.out.println("请输入各个定位点的RSSI数据");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 10; j++) {
                locRssi[i][j] = in.nextInt();
            }
        }
        for (int i = 0; i < n; i++) {
            ArrayList<KnnNode> close = KnnTest.knnPosition(locRssi[i], mapRssi);
            double x = 0, y = 0, weight = 0;
            System.out.println("距离最近的3个点的欧氏距离为：");
            for (int j = 0; j < 3; j++) {
                System.out.println("第" + close.get(j).getNum() + "号的距离：" + close.get(j).getKnnDis());
                weight = weight + close.get(j).getKnnDis();
            }
            //归一化处理
            weight = close.get(0).getKnnDis() / weight;
            for (int j = 0; j < 3; j++) {
                x = x + weight * location[close.get(j).getNum()][0];
                y = y + weight * location[close.get(j).getNum()][1];
            }
            System.out.println("预测您的位置为：" + x + "," + y);
        }
    }

    /**
     * 对文本中的内容进行处理
     * @param content 文本内容
     */
    private static void txtHandler(String content) {
        String strContent[] = content.split(",\n");
        for (int i = 0; i < strContent.length - 1; i++) {
            String num[] = strContent[i].split(",");
            for (int j = 0; j < num.length; j++) {
                int ans;
                if (j == 0) {
                    ans = Integer.parseInt(num[j].substring(1));
                } else if (j == num.length - 1) {
                    ans = Integer.parseInt(num[j].substring(0, num[j].length() - 1));
                } else {
                    ans = Integer.parseInt(num[j]);
                }
                mapRssi[i][j] = ans;
            }
        }

        String num[] = strContent[strContent.length - 1].split(",");
        for (int i = 0; i < num.length; i++) {
            int ans;
            if (i == 0) {
                ans = Integer.parseInt(num[i].substring(1));
            } else if (i == num.length - 1){
                ans = Integer.parseInt(num[i].substring(0, 3));
            } else {
                ans = Integer.parseInt(num[i]);
            }
            mapRssi[strContent.length - 1][i] = ans;
        }
    }

    private static String getTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }
}
